﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlServerCe;

namespace ProjectoPV_ESW.Account.Ementa
{
    /// <summary>
    /// Classe desenvolvida por Mário Pereira
    /// </summary>
    public partial class CriarEmentaDia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.LabelInfoGrid.Text = "Não está nenhuma ementa seleccionada.       ";
        }

        protected void ButtonOKEmentaDia_Click(object sender, EventArgs e)
        {
            String sopa = TextBox1.Text;
            String sobremesa = TextBox2.Text;

            if (sobremesa.Length != 0 && sopa.Length != 0)
            {
                string connectionStringJardim = ConfigurationManager.ConnectionStrings["JardimEntitiesString"].ConnectionString;
                string commandText = @"INSERT INTO Ementa (Sopa, Sobremesa) VALUES (@sopa, @sobremesa)";
                SqlCeConnection conn = new SqlCeConnection(connectionStringJardim);
                try
                {
                   
                    conn.Open();
                    SqlCeCommand insertCommand = new SqlCeCommand(commandText, conn);
                    insertCommand.Parameters.AddWithValue("@sopa", sopa);
                    insertCommand.Parameters.AddWithValue("@sobremesa", sobremesa);
                    int rows = insertCommand.ExecuteNonQuery();
                    insertCommand.Parameters.Clear();
                    insertCommand.Dispose();
                }
                catch (Exception exc)
                {
                    LabelErr.Text = exc.Source + "-" + exc.Message;
                }
                finally
                {
                    conn.Close();
                    Response.Redirect("CriarEmentaDia.aspx");
                }

                //Adicionar ListaPrato
            }
            else
            {

            }
        }

        protected void ButtonVoltarEmentaDia1_Click(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("CriarListaPrato.aspx");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["IdEmentaBase"] = GridView1.SelectedRow.Cells[1].Text;
            this.LabelInfoGrid.Text = "Ementa seleccionada: " + (string)Session["IdEmentaBase"];
            Button1.Visible = true;


            //1º Vai ver se existem pratos inseridos na Ementa em questão
            string connectionStringJardim = ConfigurationManager.ConnectionStrings["JardimEntitiesString"].ConnectionString;
            SqlCeConnection conn = new SqlCeConnection(connectionStringJardim);
            string selectCountCommand = @"SELECT COUNT(ListaPratosId) FROM ListaPratos WHERE [Ementa_EmentaId]= " + Session["IdEmentaBase"];
            SqlCeCommand selectCommand = new SqlCeCommand(selectCountCommand, conn);
            int numeroLinhas = 0;
            try
            {
                conn.Open();
                numeroLinhas = (int)selectCommand.ExecuteScalar();
                numeroLinhas.ToString();
            }
            catch (Exception ex)
            {
                LabelErr.Text = ex.Source + "-" + ex.Message;
            }
            finally
            {
                conn.Close();
            }
            if (numeroLinhas == 0)
            {
                TextBox3.Text = System.Web.HttpUtility.HtmlDecode("Ementa\n" + "Sopa: " + GridView1.SelectedRow.Cells[2].Text);
                TextBox3.Text += "\nSobremesa: " + System.Web.HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[3].Text);
                TextBox3.Text += "\n\nEMENTA SEM PRATOS!";
                Session["EditEmenta"] = false;
            }
            else
            {
                
                string selectPratos = "SELECT PratoId , NomePrato FROM ListaPratos , Prato WHERE Ementa_EmentaId = @idementa AND Prato_PratoId=PratoId";

                SqlCeCommand selectCommand2 = new SqlCeCommand(selectPratos, conn);
                selectCommand2.Parameters.AddWithValue("@idementa", GridView1.SelectedRow.Cells[1].Text);
                SqlCeDataReader reader;
                string prato1 = "";
                string prato2 = "";
                string prato3 = "";
                string prato4 = "";
                string prato5 = "";
                try
                {
                    conn.Open();
                    reader = selectCommand2.ExecuteReader();
                    reader.Read();
                    prato1 = reader["NomePrato"].ToString() ;
                    Prato1Id = (int)reader["PratoId"];
                    reader.Read();
                    prato2 = (string)reader["NomePrato"];
                    Prato2Id = (int)reader["PratoId"];
                    reader.Read();
                    prato3 = (string)reader["NomePrato"];
                    Prato3Id = (int)reader["PratoId"];
                    reader.Read();
                    prato4 = (string)reader["NomePrato"];
                    Prato4Id = (int)reader["PratoId"];
                    reader.Read();
                    prato5 = (string)reader["NomePrato"];
                    Prato5Id = (int)reader["PratoId"];
                }
                catch (Exception ex)
                {
                    LabelErr.Text = ex.Source + "-" + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
                TextBox3.Text ="Ementa\n" + "Sopa: " + System.Web.HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[2].Text);
                TextBox3.Text += System.Web.HttpUtility.HtmlDecode("\nSobremesa: " + GridView1.SelectedRow.Cells[3].Text);
                TextBox3.Text += System.Web.HttpUtility.HtmlDecode("\nPrato1: " + prato1);
                TextBox3.Text += System.Web.HttpUtility.HtmlDecode("\nPrato2: " + prato2);
                TextBox3.Text += System.Web.HttpUtility.HtmlDecode("\nPrato3: " + prato3);
                TextBox3.Text += System.Web.HttpUtility.HtmlDecode("\nPrato4: " + prato4);
                TextBox3.Text += System.Web.HttpUtility.HtmlDecode("\nPrato5: " + prato5);
                Session["prato1id"] = Prato1Id;
                Session["prato1id"] = Prato2Id;
                Session["prato1id"] = Prato3Id;
                Session["prato1id"] = Prato4Id;
                Session["prato1id"] = Prato5Id;
                Session["EditEmenta"] = true;
            }


        }
        public int Prato1Id { get; set; }
        public int Prato2Id { get; set; }
        public int Prato3Id { get; set; }
        public int Prato4Id { get; set; }
        public int Prato5Id { get; set; }
    }
}